package com.test.pm;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

import com.rosiminc.pm.game.PipesManiaGame;
import com.rosiminc.pm.game.Tile;

public class PMTestFrame05 extends JFrame {

	private static final long serialVersionUID = 1L;
	private final int WIDTH = 10;
	private final int HEIGHT = 5;

	private PipesManiaGame game;

	private ImageIcon[] images;
	private JLabel[][] imgTiles;

	public PMTestFrame05() {
		super();

		initializeComponents();
		setVisible(true);
		pack();

		setSize(new Dimension(400, 400));
		setExtendedState(MAXIMIZED_BOTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private void initializeComponents() {

		createImages();
		imgTiles = new JLabel[HEIGHT][WIDTH];

		game = new PipesManiaGame(HEIGHT, WIDTH);
		game.newGame();

		this.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getScrollPane() {
		JPanel scrollPanel = new JPanel(new BorderLayout());

		JScrollPane scroll = new JScrollPane(getMainGridPanel(),
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPanel.add(scroll, BorderLayout.CENTER);

		return scrollPanel;
	}

	// X XX sid = 2009130904

	private JPanel getMainGridPanel() {
		JPanel panel = new JPanel(new GridLayout(HEIGHT, WIDTH, 0, 0));

		Tile[][] board = game.getBoard();

		for (int row = 0; row < HEIGHT; row++)
			for (int col = 0; col < WIDTH; col++) {
				panel.add(getTile(row, col, board));
			}

		JPanel flowPanel = new JPanel();// FlowLayout
		flowPanel.add(panel);

		return flowPanel;
	}

	private Component getTile(int row, int col, Tile[][] board) {

		JPanel panel = new JPanel(new BorderLayout());
		//panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		panel.setBorder(BorderFactory.createEmptyBorder());

		Tile t = board[row][col];

		JLabel label = new JLabel();
		label.setIcon(images[t.getTileNumber()]);
		label.addMouseListener(new TileListener(row, col));
		
		imgTiles[row][col] = label;
		panel.add(label, BorderLayout.CENTER);
		return panel;
	}

	private void createImages() {
		images = new ImageIcon[16];

		/*int size = 30;
		BufferedImage image;
		BufferedImage buffImg;
		try {
			image = ImageIO.read(new File("images/RedPipesTr.png"));

		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

		Graphics2D gr;

		for (int i = 0; i < 16; i++) {
			buffImg = new BufferedImage(30, 30, image.getType());
			gr = buffImg.createGraphics();
			gr.drawImage(image, 0, 0, size, size, size * i, 0, size * i + size,
					size, null);
			gr.dispose();
			images[i] = new ImageIcon(buffImg);
		}

		// images = new ImageIcon[](new BufferedImage())*/
		try{
			for(int i=0; i<16;i++)
			{
				images[i] = new ImageIcon(
						ImageIO.read(new File("images/p" + i + ".bmp"))
						);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new PMTestFrame05();
			}
		});
	}

	private class TileListener extends MouseAdapter {

		private int row;
		private int col;

		public TileListener(int row, int col) {
			this.row = row;
			this.col = col;
		}

		@Override
		public void mouseClicked(MouseEvent event) {
			if (event.getButton() == MouseEvent.BUTTON1) {
				game.performActionA(row, col);
			} else if (event.getButton() == MouseEvent.BUTTON3) {
				game.performActionB(row, col);
			}
			imgTiles[row][col].setIcon(
					images[game.getTileAt(row, col).getTileNumber()]);
		}

		@Override
		public void mousePressed(MouseEvent arg0) {

		}

		@Override
		public void mouseReleased(MouseEvent arg0) {

		}

	}

}
